package com.yequan.spring.ioc.overview.dependency.injection;

import com.yequan.spring.ioc.overview.repository.UserRepository;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;


/**
 * 依赖注入实例
 *
 * @author : Administrator
 * @date : 2020/3/31
 */
public class DependencyInjectionDemo {

    public static void main(String[] args) {

        /**
         * 配置 xml 配置文件
         * 启动 Spring 应用上下文
         */
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/META-INF/dependency-injection-context.xml");

        //1.依赖来源: 自定义的bean
        UserRepository userRepository = applicationContext.getBean("userRepository", UserRepository.class);
//        System.out.println(userRepository);

        //2.依赖来源: 内建的依赖
        BeanFactory beanFactory1 = userRepository.getBeanFactory();
        System.out.println(beanFactory1);

        ObjectFactory userObjectFactory = userRepository.getUserObjectFactory();
        System.out.println(userObjectFactory.getObject() == applicationContext);

        //3.依赖来源: 容器内建的bean
        Environment environment = applicationContext.getBean(Environment.class);
        System.out.println("获取 Environment 类型的bean: " + environment);
    }

    private static void whoIsIocContainer(UserRepository userRepository,ApplicationContext applicationContext){
        System.out.println(userRepository.getBeanFactory()==applicationContext);
    }

}
